﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using ApocDev.TownsApi.XmlFiles;

using Action = ApocDev.TownsApi.XmlFiles.Action;

namespace ApocDev.TownsApi
{
    public static class ModFiles
    {
        public static string BaseDirectory { get; set; }
        public static string ModifiedDirectory { get { return Path.Combine(Path.GetDirectoryName(BaseDirectory), "Mods"); } }

        public static EventHandler<FileLoadEventArgs> FileLoadUpdate;
        public static GraphicsIni Graphics { get; set; }

        internal static void FireLoadEvent(object sender, FileLoadEventArgs args)
        {
            if (FileLoadUpdate != null)
                FileLoadUpdate(sender, args);
        }

        public static void Initialize(string baseDir)
        {
            BaseDirectory = baseDir;
            Graphics = new GraphicsIni();


            // This is loaded in a specific order. Please don't mess with it!
            Types = new TypesXml(Path.Combine(baseDir, "types.xml"));
            Priorities = new PrioritiesXml(Path.Combine(baseDir, "priorities.xml"));
            Zones = new ZonesXml(Path.Combine(baseDir, "zones.xml"));
            Effects = new EffectsXml(Path.Combine(baseDir, "effects.xml"));
            Terrain = new TerrainXml(Path.Combine(baseDir, "terrain.xml"));
            LivingEntities = new LivingEntityXml(Path.Combine(baseDir, "livingentities.xml"));
            Items = new ItemsXml(Path.Combine(baseDir, "items.xml"));
            Actions = new ActionsXml(Path.Combine(baseDir, "actions.xml"));

            Buildings = new BuildingsXml(Path.Combine(baseDir, "buildings.xml"));

            FireLoadEvent(null, new FileLoadEventArgs("Finished", 1, 1));

            //Audio.PlaySoundFile("musicMM");
        }

        public static void SaveAll()
        {
            Types.Save();
            //Priorities.Save();

            Terrain.Save();
            //Actions.Save();
            Items.Save();

            Buildings.Save().Save(Path.Combine(ModifiedDirectory, "buildings.xml"));
        }

        public static PrioritiesXml Priorities { get; set; }
        public static ActionsXml Actions { get; set; }
        public static TerrainXml Terrain { get; set; }
        public static TypesXml Types { get; set; }
        public static ItemsXml Items { get; set; }
        public static BuildingsXml Buildings { get; set; }
        public static EffectsXml Effects { get; set; }
        public static LivingEntityXml LivingEntities { get; set; }
        public static ZonesXml Zones { get; set; }

        public static Type GetType(string id)
        {
            return Types.FirstOrDefault(t => t.ID == id);
        }

        public static Action GetAction(string id)
        {
            return Actions.FirstOrDefault(a => a.ID == id);
        }

        public static Priority GetPriority(string id)
        {
            return Priorities.FirstOrDefault(p => p.ID == id);
        }

        public static Item GetItem(string id)
        {
            return Items.FirstOrDefault(i => i.ID == id);
        }

        public static Terrain GetTerrain(string id)
        {
            return Terrain.FirstOrDefault(t => t.ID == id);
        }

        public static LivingEntity GetLivingEntity(string id)
        {
            return LivingEntities.FirstOrDefault(li => li.ID == id);
        }

        public static Zone GetZone(string id)
        {
            return Zones.FirstOrDefault(z => z.ID == id);
        }

        public static Effect GetEffect(string id)
        {
            return Effects.FirstOrDefault(e => e.ID == id);
        }
    }

    public class FileLoadEventArgs:EventArgs
    {
        public FileLoadEventArgs(string fileName, int total, int current)
        {
            FileName = fileName;
            Total = total;
            Current = current;
        }

        public string FileName { get; set; }
        public int Total { get; set; }
        public int Current { get; set; }
    }
}
